<!DOCTYPE html>

<html>

<head>

    <!-- 页面meta -->

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>浩瀚机房节能管理系统</title>

    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">

    <!-- 引入样式 -->

    <link rel="stylesheet" href="../vue_demo/node_modules/element-ui/lib/theme-chalk/index.css">

    <!-- <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css"> -->

    <link rel="stylesheet" href="../css/style.css">

</head>

<body class="hold-transition">

<div id="app">

    <!-- 顶层菜单框-->
    <div class="content-header" style="display: flex; justify-content: space-between; align-items: center;">
        <h3>浩瀚机房节能管理系统</h3>
    </div>

    <!-- 整体列表 -->
    <div class="app-container">
        <!-- 整体列表 -->
        <div class="box">

            <!-- 搜索 和 批量操作 -->
            <div class="filter-container">
                <el-row type="flex">
                    <!-- 批量操作 -->
                    <el-col :span="12">
                        <div class="grid-content bg-purple">
                            <el-button type="primary" size="mini" @click="handleCreate()" icon="el-icon-circle-plus" round>新增服务器</el-button>
                            <el-button @click="batchStartup()" type="success" size="mini" round><i class="el-icon-caret-right"></i>批量开机</el-button>
                            <el-button @click="batchShutdown()" type="danger" size="mini" round><i class="el-icon-switch-button"></i>批量关机</el-button>
                            <el-button @click="batchUpdateIpmiTime()" type="success" size="mini" round><i class="el-icon-setting"></i>批量修改开关机定时</el-button>
                        </div>
                    </el-col>
                    <!-- 搜索 -->
                    <el-col :span="12" style="text-align: right;">
                        <div>
                        <el-select v-model="pagination.department"  placeholder="请选择部门" style="width: 200px;"
                                   class="filter-item" clearable>
                            <el-option
                                    v-for="department in departments"
                                    :key="department.id"
                                    :label="department.name"
                                    :value="department.name">
                            </el-option>
                        </el-select>
                        <el-select placeholder="请选择机柜" v-model="pagination.rack" style="width: 200px;" class="filter-item" clearable >
                            <el-option
                                    v-for="rack in racks"
                                    :key="rack.id"
                                    :label="rack.name"
                                    :value="rack.name">
                            </el-option>
                        </el-select>
                        <el-input placeholder="服务器IP" v-model="pagination.serverIp" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="getAll()" type="primary" icon="el-icon-search">查询</el-button>
                        </div>
                    </el-col>
                </el-row>
            </div>

            <!-- 数据表格 -->
            <el-table size="small"
                      :data="dataList"
                      stripe highlight-current-row resizable
                      :default-sort = "{prop: 'rack', order: 'ascending'}"
                      @selection-change="handleSelectionChange" style="width: 100%"
                      row-key="id" border default-expand-all
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column type="selection" width="55" align="center" > </el-table-column>
                <el-table-column type="index" align="center" label="序号" fixed></el-table-column>
                <el-table-column prop="serverIp" label="服务器IP" align="center" fixed sortable></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="department" label="部门" align="center"></el-table-column>
                <el-table-column prop="rack" label="机柜" align="center" sortable></el-table-column>
                <el-table-column prop="os" label="操作系统" align="center"></el-table-column>
                <el-table-column prop="architecture" label="CPU型号" align="center"></el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                    <template slot-scope="scope">
                        {{ getStatusText(scope.row.status) }}
                    </template>
                </el-table-column>
                <el-table-column prop="runTime" label="运行时间" align="center"></el-table-column>
                <el-table-column prop="bmcIp" label="管理口IP" align="center"></el-table-column>
                <el-table-column prop="bmcUser" label="管理口用户名" align="center"></el-table-column>
                <el-table-column prop="remark" label="其他说明" align="center"></el-table-column>

                 <!-- 操作列-->
                <el-table-column label="操作" align="center" fixed="right" width="500">
                    <template slot-scope="scope">
                        <div style="display: flex; align-items: center; gap: 8px;">
                            <template v-if="scope.row.serverType == '物理机'">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" icon="el-icon-edit-outline"></el-button>
                                <el-button type="danger" size="mini" @click="handleDelete(scope.row)" icon="el-icon-delete"></el-button>
                                <!-- 开机-->
                                <el-button v-if="scope.row.status === 0" type="success" size="mini"
                                           @click="handleStartup(scope.row)" :loading="rowLoading[scope.row.id]?.loading"
                                           icon="el-icon-caret-right"></el-button>
                                <el-button v-else type="info" size="mini" icon="el-icon-caret-right" disabled></el-button>

                                <!-- 关机-->
                                <el-button v-if="scope.row.status === 1" type="danger" size="mini"
                                           @click="handleShutdown(scope.row)" :loading="rowLoading[scope.row.id]?.loading"
                                           icon="el-icon-switch-button"></el-button>
                                <el-button v-else type="info" size="mini" icon="el-icon-switch-button" disabled></el-button>
                                <!-- 定时开关-->
                                <el-tooltip content="是否定时开关机" placement="top">
                                    <el-switch v-model="scope.row.ipmiFlag" @change="changeStatus(scope.row)">
                                    </el-switch>
                                </el-tooltip>

                                <el-tooltip content="开机时间" placement="top">
                                    <el-time-select
                                            v-model="scope.row.startupTime" size="mini"
                                            :picker-options="{start: '01:30',    end: '10:30'  }"
                                            value-format="HH:mm"  format="HH:mm"
                                            placeholder="开机时间" :clearable="false"
                                            :disabled="!scope.row.ipmiFlag"
                                            @change="handleTimeChange(scope.row)">
                                    </el-time-select>
                                </el-tooltip>
                                <el-tooltip content="关机时间" placement="top">
                                    <el-time-select
                                            arrow-control
                                            v-model="scope.row.shutdownTime" size="mini"
                                            :picker-options="{ start: '18:30', end: '23:00' }"
                                            format="HH:mm"  value-format="HH:mm"
                                            :disabled="!scope.row.ipmiFlag"
                                            placeholder="关机时间" :clearable="false"
                                            @change="handleTimeChange(scope.row)">
                                    </el-time-select>
                                </el-tooltip>
                            </template>

                            <template v-else-if="scope.row.serverType == '板卡'">
                                <template v-if="scope.row.rmcp === 'ipmi'">
                                    <!-- 开机-->
                                    <el-button v-if="scope.row.status === 0" type="success" size="mini"
                                               @click="handleStartup(scope.row)" :loading="rowLoading[scope.row.id]?.loading"
                                               icon="el-icon-caret-right"></el-button>
                                    <el-button v-else type="info" size="mini" icon="el-icon-caret-right" disabled></el-button>

                                    <!-- 关机-->
                                    <el-button v-if="scope.row.status === 1" type="danger" size="mini"
                                               @click="handleShutdown(scope.row)" :loading="rowLoading[scope.row.id]?.loading"
                                               icon="el-icon-switch-button"></el-button>
                                    <el-button v-else type="info" size="mini" icon="el-icon-switch-button" disabled></el-button>
                                    <!-- 定时开关-->
                                    <el-tooltip content="是否定时开关机" placement="top">
                                        <el-switch v-model="scope.row.ipmiFlag" @change="changeStatus(scope.row)">
                                        </el-switch>
                                    </el-tooltip>

                                    <el-tooltip content="开机时间" placement="top">
                                        <el-time-select
                                                v-model="scope.row.startupTime" size="mini"
                                                :picker-options="{start: '01:30',    end: '10:30'  }"
                                                value-format="HH:mm"  format="HH:mm"
                                                placeholder="开机时间" :clearable="false"
                                                :disabled="!scope.row.ipmiFlag"
                                                @change="handleTimeChange(scope.row)"
                                                style="width: 95px;">
                                        </el-time-select>
                                    </el-tooltip>
                                    <el-tooltip content="关机时间" placement="top">
                                        <el-time-select
                                                arrow-control
                                                v-model="scope.row.shutdownTime" size="mini"
                                                :picker-options="{ start: '18:30', end: '23:00' }"
                                                format="HH:mm"  value-format="HH:mm"
                                                :disabled="!scope.row.ipmiFlag"
                                                placeholder="关机时间" :clearable="false"
                                                @change="handleTimeChange(scope.row)"
                                                style="width: 95px;">
                                        </el-time-select>
                                    </el-tooltip>
                                </template>
                            </template>

                            <template v-else>
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" icon="el-icon-edit-outline"></el-button>
                                <el-button type="danger" size="mini" @click="handleDelete(scope.row)" icon="el-icon-delete"></el-button>
                                <el-tooltip content="手动更新板卡信息" placement="top">
                                    <el-button type="primary" size="mini" @click="updateBroadCard(scope.row)" icon="el-icon-refresh-right"></el-button>
                                </el-tooltip>
                            </template>
                        </div>
                    </template>
                </el-table-column>

            </el-table>

            <!--分页组件-->
            <div class="pagination-container">

                <el-pagination
                        class="pagiantion"

                        @current-change="handleCurrentChange"

                        :current-page="pagination.currentPage"

                        :page-size="pagination.pageSize"

                        layout="total, prev, pager, next, jumper"

                        :total="pagination.total">

                </el-pagination>

            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">

                <el-dialog title="新增服务器" :visible.sync="dialogFormVisible">

                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="服务器IP" prop="serverIp">
                                    <el-input v-model="formData.serverIp"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="部门" prop="department">
                                    <el-select v-model="formData.department" placeholder="请选择部门">
                                        <el-option
                                                v-for="department in departments"
                                                :key="department.id"
                                                :label="department.name"
                                                :value="department.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机柜" prop="rack">
                                    <el-select v-model="formData.rack" placeholder="请选择机柜">
                                        <el-option
                                                v-for="rack in racks"
                                                :key="rack.id"
                                                :label="rack.name"
                                                :value="rack.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="服务器类型" prop="serverType">
                                    <el-select v-model="formData.serverType" placeholder="请选择服务器类型">
                                        <el-option
                                                v-for="serverType in serverTypes"
                                                :key="serverType.id"
                                                :label="serverType.name"
                                                :value="serverType.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <!-- 管理口相关信息 -->
                        <template v-if="formData.serverType !== '探针'">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="管理口IP">
                                        <el-input v-model="formData.bmcIp"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="管理口用户名">
                                        <el-input v-model="formData.bmcUser"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="管理口密码">
                                        <el-input v-model="formData.bmcPass"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="其他说明">
                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </template>
                        <template v-else>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="其他说明">
                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </template>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="cancel()">取消</el-button>

                        <el-button type="primary" @click="handleAdd()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">

                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="服务器IP" prop="serverIp">
                                    <el-input v-model="formData.serverIp"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="部门" prop="department">
                                    <el-select v-model="formData.department" placeholder="请选择部门">
                                        <el-option
                                                v-for="department in departments"
                                                :key="department.id"
                                                :label="department.name"
                                                :value="department.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机柜" prop="rack">
                                    <el-select v-model="formData.rack" placeholder="请选择机柜">
                                        <el-option
                                                v-for="rack in racks"
                                                :key="rack.id"
                                                :label="rack.name"
                                                :value="rack.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="服务器类型" prop="serverType">
                                    <el-select v-model="formData.serverType" placeholder="请选择服务器类型">
                                        <el-option
                                                v-for="serverType in serverTypes"
                                                :key="serverType.id"
                                                :label="serverType.name"
                                                :value="serverType.name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <!-- 管理口相关信息 -->
                        <template v-if="formData.serverType !== '探针'">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="管理口IP">
                                        <el-input v-model="formData.bmcIp"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="管理口用户名">
                                        <el-input v-model="formData.bmcUser"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="管理口密码">
                                        <el-input v-model="formData.bmcPass"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="其他说明">
                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </template>
                        <template v-else>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="其他说明">
                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </template>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="cancel()">取消</el-button>

                        <el-button type="primary" @click="handleEdit()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

           <!-- 批量修改开关机时间弹层-->
            <div class="add-from">
                <el-dialog title="批量修改开机时间" :visible.sync="dialogFormVisible4Batch">
                    <el-form ref="timeEditForm"  label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="定时开机时间">
                                    <el-time-select
                                            v-model="startupTime"
                                            :picker-options="{start: '01:30',    end: '09:30'  }"
                                            value-format="HH:mm"
                                            format="HH:mm"
                                            placeholder="开机时间">
                                    </el-time-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="定时关机时间">
                                    <el-time-select
                                            arrow-control
                                            v-model="shutdownTime"
                                            :picker-options="{ start: '18:30', end: '22:30' }"
                                            format="HH:mm"
                                            value-format="HH:mm"
                                            placeholder="关机时间">
                                   </el-time-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">

                        <el-button @click="cancel()">取消</el-button>

                        <el-button type="primary" @click="handleBatchUpdateIpmiTime()">确定</el-button>

                    </div>
                </el-dialog>
            </div>

            <!-- 进度条弹出 -->
            <div class="add-from">
                <el-dialog title="进度条" :visible.sync="showProgress">
                    <el-progress :text-inside="true" :stroke-width="16" :percentage="progress"></el-progress>
                </el-dialog>
            </div>

        </div>

    </div>

</div>

</body>

<!-- 引入组件库 -->

<script src="../js/vue.js"></script>

<script src="../vue_demo/node_modules/element-ui/lib/index.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>

<script src="../vue_demo/node_modules/axios/dist/axios.js"></script>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            startupTime: "",
            shutdownTime: "",
            serverIds: [],
            //当前页要展示的列表数据
            dataList: [
                    {
                    "id": 145,
                    "serverIp": "10.10.3.43",
                    "username": "root",
                    "password": "tma1100@jszx",
                    "rack": "A03",
                    "department": "应用产品部",
                    "os": "openEuler 22.03 LTS",
                    "architecture": "x86_64",
                    "status": 1,
                    "runTime": "2 days 22 hours 58 minutes",
                    "bmcIp": "10.10.80.43",
                    "bmcUser": "root",
                    "bmcPass": "tma@1100",
                    "remark": "",
                    "ipmiFlag": false,
                    "startupTime": "08:00",
                    "shutdownTime": "21:30",
                    "rmcp": "lanplus",
                    "connectException": "",
                    "children": null,
                    "foreignIp": null,
                    "foreignUser": null,
                    "foreignPass": null,
                    "serverType": "物理机"
                    },
                    {
                        "id": 174,
                        "serverIp": "10.10.21.90",
                        "username": "root",
                        "password": "tma@1100",
                        "rack": "A01",
                        "department": "平台产品部",
                        "os": "BigCloud Enterprise Linux For Euler 22.10U1 LTS",
                        "architecture": "x86_64",
                        "status": 1,
                        "runTime": "3 weeks 1 day 5 hours 10 minutes",
                        "bmcIp": null,
                        "bmcUser": null,
                        "bmcPass": null,
                        "remark": null,
                        "ipmiFlag": false,
                        "startupTime": "07:00",
                        "shutdownTime": "22:00",
                        "rmcp": "lanplus",
                        "connectException": "",
                        "children": [
                        {
                            "id": 211,
                            "serverIp": "192.168.0.104",
                            "username": "root",
                            "password": "tma1100",
                            "rack": "A01",
                            "department": "平台产品部",
                            "architecture": null,
                            "os": null,
                            "status": 1,
                            "runTime": " 6:21",
                            "bmcIp": "00:30:64:69:3a:e4",
                            "bmcUser": "root",
                            "bmcPass": "4",
                            "remark": null,
                            "ipmiFlag": true,
                            "startupTime": "07:00",
                            "shutdownTime": "22:00",
                            "rmcp": "ipmi",
                            "serverType": "板卡",
                            "foreignIp": "10.10.21.90",
                            "foreignUser": "root",
                            "foreignPass": "tma@1100"
                        },
                        {
                            "id": 212,
                            "serverIp": "192.168.0.110",
                            "username": "root",
                            "password": "tma1100",
                            "rack": "A01",
                            "department": "平台产品部",
                            "architecture": null,
                            "os": null,
                            "status": 1,
                            "runTime": " 6:20",
                            "bmcIp": "00:30:64:8b:2e:a3",
                            "bmcUser": "root",
                            "bmcPass": "10",
                            "remark": null,
                            "ipmiFlag": true,
                            "startupTime": "07:00",
                            "shutdownTime": "22:00",
                            "rmcp": "ipmi",
                            "serverType": "板卡",
                            "foreignIp": "10.10.21.90",
                            "foreignUser": "root",
                            "foreignPass": "tma@1100"
                        }
                        ],
                        "foreignIp": null,
                        "foreignUser": null,
                        "foreignPass": null,
                        "serverType": "探针"
                    },
                    {
                        "id": 152,
                        "serverIp": "10.10.24.121",
                        "username": "root",
                        "password": "haohandataadmin",
                        "rack": "B16",
                        "department": "平台产品部",
                        "os": "CentOS Linux 7 ",
                        "architecture": "x86_64",
                        "status": 1,
                        "runTime": "5 hours 55 minutes",
                        "bmcIp": "10.10.80.128",
                        "bmcUser": "admin",
                        "bmcPass": "tma1100",
                        "remark": "",
                        "ipmiFlag": true,
                        "startupTime": "08:00",
                        "shutdownTime": "20:30",
                        "rmcp": "lanplus",
                        "connectException": "",
                        "children": null,
                        "foreignIp": null,
                        "foreignUser": null,
                        "foreignPass": null,
                        "serverType": "物理机"
                    }
                    ],
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            dialogFormVisible4Batch: false, //批量修改开关机时间是否可见
            formData: {},//表单数据
            multipleSelection: [], //多选
            rowLoading: {},  // 用于存储每行的 loading 状态
            // 进度条相关变量
            progress: 0,
            intervalId: null,
            showProgress: false,
            threshold: 12, // 进度条隐藏的阈值
            rules: {//校验规则
                serverIp: [{ required: true, message: '服务器IP为必填项', trigger: 'blur' }],
                username: [{ required: true, message: '服务器用户名为必填项', trigger: 'blur' }],
                password: [{ required: true, message: '服务器密码为必填项', trigger: 'blur' }],
                department: [
                    { required: true, message: '请选择部门', trigger: 'change' }
                ],
                rack: [{ required: true, message: '机柜为必填项', trigger: 'change' }],
                serverType: [
                    { required: true, message: '请选择服务器类型', trigger: 'change' }
                ]
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                department: "",
                rack: "",
                serverIp: ""
            },
            departments: [
              { id: 1, name: '应用产品部' },
              { id: 2, name: '平台产品部' },
              { id: 3, name: '行业产品部' },
              { id: 4, name: 'CTO办公室' },
              { id: 5, name: '测试部' }
            ],
            racks: [
              { id: 1, name: 'A01' }, { id: 2, name: 'A02' }, { id: 3, name: 'A03' }, { id: 4, name: 'A04' },
              { id: 5, name: 'A05' }, { id: 6, name: 'A06' }, { id: 7, name: 'A07' }, { id: 8, name: 'A08' },
              { id: 9, name: 'A09' },{ id: 10, name: 'A10' }, { id: 11, name: 'A11' }, { id: 12, name: 'A12' },
              { id: 13, name: 'A13' }, { id: 14, name: 'A14' }, { id: 15, name: 'A15' }, { id: 16, name: 'A16' },
              { id: 17, name: 'A17' }, { id: 18, name: 'A18' }, { id: 19, name: 'A19' }, { id: 20, name: 'A20' },
              { id: 21, name: 'A21' }, { id: 22, name: 'A22' }, { id: 23, name: 'A23' }, { id: 24, name: 'A24' },
              { id: 25, name: 'B01' }, { id: 26, name: 'B02' }, { id: 27, name: 'B03' }, { id: 28, name: 'B04' },
              { id: 29, name: 'B05' }, { id: 30, name: 'B06' }, { id: 31, name: 'B07' }, { id: 32, name: 'B08' },
              { id: 33, name: 'B09' }, { id: 40, name: 'B10' }, { id: 41, name: 'B11' }, { id: 42, name: 'B12' },
              { id: 43, name: 'B13' }, { id: 44, name: 'B14' }, { id: 45, name: 'B15' }, { id: 46, name: 'B16' },
              { id: 47, name: 'B17' }, { id: 48, name: 'B18' }, { id: 49, name: 'B19' }, { id: 50, name: 'B20' },
              { id: 51, name: 'B21' }, { id: 52, name: 'B22' }
            ],
            serverTypes: [
              { id: 1, name: '物理机' }, { id: 2, name: '探针' }
            ]
        },

        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //调用查询全部数据的操作
            this.getAll();
            // console.log(JSON.stringify(this.dataList));
        },

        methods: {

            //分页查询
            getAll() {
                //组织参数，拼接url请求地址
                param = "?department="+this.pagination.department;
                param +="&rack="+this.pagination.rack;
                param +="&serverIp="+this.pagination.serverIp;


                //发送异步请求
                axios.get("/servers/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{
                    // console.log(res.data);
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;

                    this.dataList = res.data.data.records;
                });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //修改页码值为当前选中的页码值
                this.pagination.currentPage = currentPage;
                //执行查询
                this.getAll();
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
            },

            //添加
            handleAdd() {
                axios.post("/servers",this.formData).then((res)=>{
                    //判断当前操作是否成功
                    if(res.data.flag){
                        //1.关闭弹层
                        this.dialogFormVisible = false;
                        this.$message.success(res.data.msg);
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).finally(()=>{
                    //2.重新加载数据
                    this.getAll();
                });
            },

            //取消
            cancel(){
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                this.dialogFormVisible4Batch = false;
                this.$message.info("当前操作取消");
            },

            // 删除
            handleDelete(row) {
                // console.log(row);
                this.$confirm("此操作永久删除当前信息，是否继续？","提示",{type:"info"}).then(()=>{
                    axios.delete("/servers/"+row.id).then((res)=>{
                        if(res.data.flag){
                            this.$message.success("删除成功");
                        }else{
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(()=>{
                        //2.重新加载数据
                        this.getAll();
                    });
                }).catch(()=>{
                    this.$message.info("取消操作");
                });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                axios.get("/servers/"+row.id).then((res)=>{
                    if(res.data.flag && res.data.data != null ){
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;
                    }else{
                        this.$message.error("数据同步失败，自动刷新");
                    }
                }).finally(()=>{
                    //2.重新加载数据
                    this.getAll();
                });
            },

            //修改
            handleEdit() {
                axios.put("/servers",this.formData).then((res)=>{
                    //判断当前操作是否成功
                    if(res.data.flag){
                        //1.关闭弹层
                        this.dialogFormVisible4Edit = false;
                        this.$message.success("修改成功");
                    }else{
                        this.$message.error("修改失败");
                    }
                }).finally(()=>{
                    //2.重新加载数据
                    this.getAll();
                });
            },

            // 状态映射显示
            getStatusText(status) {
                switch (status) {
                    case 0:
                        return '关机';
                    case 1:
                        return '开机';
                    case 2:
                        return '密码异常';
                    default:
                        return '未知状态';
                }
            },

            // 单台启动
            handleStartup(row) {
               // 判断bmcIP bmcUser bmcPass 是否为空 为空提示，有信息时发送请求到后台
               if ( (row.bmcIp == null || row.bmcIp.trim() === '') &&
                    (row.bmcUser == null || row.bmcUser.trim() === '') &&
                    (row.bmcPass == null || row.bmcPass.trim() === '')
                  ){
                   this.$message.error('请先配置管理口信息');
               }else {
                   // 设置当前行的 loading 为 true，并设置文本为“开机中...”
                   this.$set(this.rowLoading, row.id, { loading: true, text: '开机中...' });
                   setTimeout(() => {
                       axios.get('/ipmi/startup/' + row.id).then((res) => {
                           if (res.data.flag) {
                               this.$message.success('开机指令执行成功, 请稍后查看状态');
                           } else {
                               this.$message.error('开机指令执行失败，请检查管理口信息');
                           }
                       }).finally(()=>{
                          // 重新加载数据
                          this.$set(this.rowLoading, row.id, { loading: false, text: '开机' });
                          this.getAll();
                       });
                   }, 5000);
               }
            },
            // 单台关机
            handleShutdown(row) {
               // 判断bmcIP bmcUser bmcPass 是否为空 为空提示，有信息时发送请求到后台
               if ( (row.bmcIp == null || row.bmcIp.trim() === '') &&
                    (row.bmcUser == null || row.bmcUser.trim() === '') &&
                    (row.bmcPass == null || row.bmcPass.trim() === '')
                  ){
                   this.$message.error('请先配置管理口信息');
               }else {
                   this.$confirm("此操作将关闭服务器，可能会造成数据丢失，是否继续？","提示",{type:"info"}).then(()=>{
                       // 设置当前行的 loading 为 true，并设置文本为“关机中...”
                       this.$set(this.rowLoading, row.id, { loading: true, text: '关机中...' });
                       setTimeout(() => {
                           axios.get('/ipmi/shutdown/' + row.id).then((res) => {
                               if (res.data.flag) {
                                   this.$message.success('关机成功');
                               } else {
                                   this.$message.error('关机失败');
                               }
                           }).catch((err) => {
                               this.$message.error('关机失败');
                           }).finally(()=>{
                              // 重新加载数据
                              this.$set(this.rowLoading, row.id, { loading: false, text: '关机' });
                              this.getAll();
                           });
                       }, 5000);

                   }).catch(()=>{
                        this.$message.info("取消操作");
                   });
               }
            },

            // 修改定时开关机时间
            handleTimeChange(row) {
                axios.put("/servers/updateTime", row).then((res) => {
                    if (res.data.flag) {
                        this.$message.success("时间更新成功");
                    } else {
                        this.$message.error("时间更新失败");
                    }
                }).catch((err) => {
                    this.$message.error("更新失败");
                });
            },

            // 表格多选
            handleSelectionChange(val) {
               this.multipleSelection = val;
            },

            // 批量关机
            batchShutdown() {
                if (this.multipleSelection == '') {
                    this.$notify({
                        title: '温馨提示：',
                        message: '您未选中任何机器，请重新操作！！！',
                        type: 'warning'
                    });
                    return;
                }
                let checkArr = this.multipleSelection;  // multipleSelection存储了勾选到的数据
                var nums = new Array();
                checkArr.forEach(function (item) {
                   // 添加所有需要删除数据的id到一个数
                   nums.push(item.id)
                })
                this.$confirm("此操作将关闭服务器，可能会造成数据丢失，是否继续？","提示",{type:"info"}).then(()=>{
                    axios.put("/ipmi/shutdown/batch" , {ids: nums}).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                        }else{
                            this.$message.error(res.data.msg);
                        }
                    }).finally(()=>{
                        //2.重新加载数据
                        this.multipleSelection = '';
                        this.getAll();
                    });
                }).catch(()=>{
                        this.$message.info("取消操作");
                });
            },

            // 批量开机
            batchStartup() {
                if (this.multipleSelection == '') {
                    this.$notify({
                        title: '温馨提示：',
                        message: '您未选中任何机器，请重新操作！！！',
                        type: 'warning'
                    });
                    return;
                }
                let checkArr = this.multipleSelection;  // multipleSelection存储了勾选到的数据
                var nums = new Array();
                checkArr.forEach(function (item) {
                   // 添加所有需要删除数据的id到一个数
                   nums.push(item.id)
                })

                axios.put("/ipmi/startup/batch" , {ids: nums}).then((res)=>{
                    if(res.data.flag){
                        this.$message.success(res.data.msg);
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).finally(()=>{
                    //2.重新加载数据
                    this.multipleSelection = '';
                    this.getAll();
                });
            },

            // 批量修改开关机时间
            batchUpdateIpmiTime() {
                if (this.multipleSelection == '') {
                    this.$notify({
                        title: '温馨提示：',
                        message: '您未选中任何机器，请重新操作！！！',
                        type: 'warning'
                    });
                    return;
                }
                let checkArr = this.multipleSelection;  // multipleSelection存储了勾选到的数据
                var nums = new Array();
                checkArr.forEach(function (item) {
                   // 添加所有需要删除数据的id到一个数
                   nums.push(item.id)
                });

                this.serverIds = nums;
                this.dialogFormVisible4Batch = true;
                this.startupTime = "08:00";
                this.shutdownTime = "22:00";

            },

            handleBatchUpdateIpmiTime(){
                //console.log(this.serverIds);

                axios.put("/servers/batchUpdateTime" , {ids: this.serverIds , startupTime: this.startupTime , shutdownTime: this.shutdownTime}).then((res)=>{
                    if(res.data.flag){
                        this.dialogFormVisible4Batch = false;
                        this.$message.success(res.data.msg);
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).finally(()=>{
                    //2.重新加载数据
                    this.multipleSelection = '';
                    this.getAll();
                });

            },

            // 定时控制
            changeStatus(row){
                axios.put("/servers/changeAutoIpmi", {id: row.id, ipmiFlag: row.ipmiFlag}).then((res) => {
                    if(row.ipmiFlag){
                        this.$message.success('【加入】定时开关机队列');
                    }else{
                        this.$message.success('【移除】定时开关机队列');
                    }
                });
            },

            // 更新板卡信息
            updateBroadCard(row){
                console.log(row);
                axios.put("/servers/updateBroadCard", row).then((res) => {
                    if(res.data.flag){
                        this.$message.success(res.data.msg);
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).finally(()=>{
                    this.getAll();
                });

                this.startProgressTask();
            },
            startProgressTask() {
                console.log('开始执行进度任务');
                this.showProgress = true;
                this.progress = 0;
                this.intervalId = setInterval(() => {
                    this.fetchProgress();
                }, 1000);
            },
            fetchProgress() {
                axios.get('/servers/progress')
                  .then(response => {
                        this.progress = response.data;
                        console.log('当前进度:', this.progress);
                        if (this.progress >= this.threshold) {
                            clearInterval(this.intervalId);
                            setTimeout(() => {
                                this.showProgress = false;
                            },1000)
                        }
                    })
                  .catch(error => {
                        console.error('获取进度失败:', error);
                        clearInterval(this.intervalId);
                        this.showProgress = false;
                    });
            }
        },
        computed: {
            roundedPercentage() {
                return Math.round(this.progress / 12 * 100);
            }
        },
        beforeDestroy() {
            if (this.intervalId) {
                clearInterval(this.intervalId);
            }
        }
    })

</script>

<style>
    input[aria-hidden="true"]{
        display: none !important;
    }
</style>

</html>
